package com.itfreer.gis.style;

import com.itfreer.form.api.PageInfo;
import com.itfreer.gis.map.Application;
import com.itfreer.gis.map.style.*;
import com.itfreer.gis.map.style.entity.*;
import com.itfreer.utils.ResponseEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TenantAuthorizationMap {

	@Autowired
	private RestMapStyleDirectoryService mapStyleDirectoryService;

	@Autowired
	private RestMapStyleService mapStyleService;

	@Autowired
	private RestMapStyleSpriteService mapStyleSpriteService;

	@Autowired
	private RestMapStyleLayerService mapStyleLayerService;

	// private String tenantID = "4005ba3226554bb2990a565f11c2ce50";
	// private String tenantName = "广州森农源生态科技有限公司";
//	private String tenantID = "b40d4d71a1544faeac71a9682af6f7d9";
//	private String tenantName = "湖南省桃源县林业与草源局";

//	private String tenantID = "5855c5593fac4babb74f5dbf46531c3f";
//	private String tenantName = "惠东县林业";

	private String tenantID = "407725daab044c11924a9acf78d692f1";
	private String tenantName = "广州市林业";

	private Set<MapStyleLayerEntity> layers = new LinkedHashSet<>();
	private Set<MapStyleSourceEntity> sources = new LinkedHashSet<>();
	private String[] spriteIds = new String[] {};
	private Map<String, String> spriteMap = new HashMap<>();

	@Test
	public void test() throws Exception {

		Map<String, Object> where = new HashMap<>();
		where.put("tenantID", "762d36580fc241b9a624253346cdf17a");
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(1000);
		pageInfo.setWhere(where);

		ResponseEntity<List<MapStyleEntity>> styles = mapStyleService.getEntitys(pageInfo);

		List<MapStyleEntity> styleList = styles.getData();
		spriteIds = new String[styleList.size()];
		for (int i = 0; i < styleList.size(); i++) {
			layers.clear();
			sources.clear();
			styleList.get(i).setTenantID(tenantID);
			styleList.get(i).setTenantName(tenantName);
			String styleId = UUID.randomUUID().toString().replace("-", "");
			styleList.get(i).setId(styleId);

			// 获取其样式字典重新赋值
			ResponseEntity<MapStyleDirectoryEntity> directory = mapStyleDirectoryService
					.getEntity(styleList.get(i).getStyleId());
			MapStyleDirectoryEntity mapStyleDirectoryEntity = directory.getData();
			String directoryId = UUID.randomUUID().toString().replace("-", "");
			mapStyleDirectoryEntity.setId(directoryId);
			mapStyleDirectoryEntity.setTenantID(tenantID);
			mapStyleDirectoryEntity.setTenantName(tenantName);
			mapStyleDirectoryService.add(mapStyleDirectoryEntity);

			styleList.get(i).setStyleId(directoryId);

			// 获取其雪碧图重新赋值
			ResponseEntity<MapStyleSpriteEntity> sprite = mapStyleSpriteService.getEntity(styleList.get(i).getSprite());
			MapStyleSpriteEntity mapStyleSpriteEntity = sprite.getData();

			// 重复的则直接设置id，未重复则复制生成
			String spriteId = "";
			boolean isHave = false;
			for (String s : spriteIds) {
				if (mapStyleSpriteEntity.getId().equals(s)) {
					isHave = true;
					spriteId = s;
				}
			}

			if (isHave == false) {
				spriteIds[i] = mapStyleSpriteEntity.getId();
				spriteId = UUID.randomUUID().toString().replace("-", "");
				spriteMap.put(mapStyleSpriteEntity.getId(), spriteId);
				mapStyleSpriteEntity.setId(spriteId);
				mapStyleSpriteEntity.setTenantID(tenantID);
				mapStyleSpriteEntity.setTenantName(tenantName);
				mapStyleSpriteService.add(mapStyleSpriteEntity);
			} else {
				if (spriteMap.containsKey(spriteId)) {
					spriteId = spriteMap.get(spriteId);
				}
			}
			styleList.get(i).setSprite(spriteId);

			// 获取图层数据源重新赋值
			Iterator<?> sourceIterator = styleList.get(i).getSources().iterator();
			while (sourceIterator.hasNext()) {
				MapStyleSourceEntity source = (MapStyleSourceEntity) sourceIterator.next();
				source.setId(UUID.randomUUID().toString().replace("-", ""));
				source.setTenantID(tenantID);
				source.setTenantName(tenantName);
				source.setStyleId(styleId);
				sources.add(source);
			}
			styleList.get(i).setSources(sources);

			// 获取图层数据重新赋值
			Iterator<?> layerIterator = styleList.get(i).getLayers().iterator();

			while (layerIterator.hasNext()) {
				MapStyleLayerEntity layer = (MapStyleLayerEntity) layerIterator.next();
				// 树形数据id随机生成，pid关系重新对应
				if (layer.getPid().equals("0")) {
					String oldId = layer.getId();
					String oldStyleId = layer.getStyleId();
					layer.setTenantID(tenantID);
					layer.setTenantName(tenantName);
					layer.setStyleId(styleId);
					String layerId = UUID.randomUUID().toString().replace("-", "");
					layer.setId(layerId);
					layers.add(layer);

					changeChildLayer(oldStyleId, styleId, oldId, layerId);
				}
			}

			styleList.get(i).setLayers(layers);

			mapStyleService.add(styleList.get(i));
		}

	}

	private void changeChildLayer(String oldStyleId, String newStyleId, String pid, String newLayerId)
			throws IOException {
		Map<String, Object> where = new HashMap<>();
		where.put("styleId", oldStyleId);
		where.put("pid", pid);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(1000);
		pageInfo.setWhere(where);
		ResponseEntity<List<MapStyleLayerEntity>> styleLayers = mapStyleLayerService.getEntitys(pageInfo);

		for (int j = 0; j < styleLayers.getData().size(); j++) {
			MapStyleLayerEntity layer = styleLayers.getData().get(j);
			layer.setTenantID(tenantID);
			layer.setTenantName(tenantName);
			layer.setStyleId(newStyleId);
			String layerId = layer.getId();
			layer.setPid(newLayerId);
			String uuid = UUID.randomUUID().toString().replace("-", "");
			layer.setId(uuid);
			layers.add(layer);
			changeChildLayer(oldStyleId, layer.getStyleId(), layerId, uuid);
		}
	}

}
